﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Threading;
using System.Windows.Media.Imaging;
using BlobTowerDefense.Tools;
using System.Globalization;
using BlobTowerDefense.Managers;
using BlobTowerDefense.Resources;

namespace BlobTowerDefense
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
            Thread.CurrentThread.CurrentCulture = Loader.Load_Culture();
        }

        //testovaci komentar

        private void buttonContinue_Click(object sender, RoutedEventArgs e)
        {            
            animation_Menu_Hide_FromDown.Begin();
            animation_Menu_Hide_FromDown.Completed += new EventHandler(animation_Menu_Hide_FromDown_Completed_Continue);            
        }

        void animation_Menu_Hide_FromDown_Completed_Continue(object sender, EventArgs e)
        {
            animation_Menu_Hide_FromDown.Completed -= new EventHandler(animation_Menu_Hide_FromDown_Completed_Continue);

            NavigationService.Navigate(new Uri(String.Format("/Pages/GamePage.xaml?Difficulty={0}&Continue={1}", "EASY", 1), UriKind.Relative));
        }

        private void buttonNewGame_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show(ApplicationResources.Get("DoYouWantNewGame_Label"), ApplicationResources.Get("StartNewGame_Label"), MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                animation_Menu_Hide_FromDown.Completed += new EventHandler(animation_Menu_Hide_FromDown_Completed_NewGame);
                animation_Menu_Hide_FromDown.Begin();
            }
        }

        void animation_Menu_Hide_FromDown_Completed_NewGame(object sender, EventArgs e)
        {
            animation_Menu_Hide_FromDown.Completed -= new EventHandler(animation_Menu_Hide_FromDown_Completed_NewGame);
            NavigationService.Navigate(new Uri("/Pages/SelectDifficultyPage.xaml", UriKind.Relative));
        }


        private void buttonSettings_Click(object sender, RoutedEventArgs e)
        {
            animation_Menu_Hide_FromDown.Completed += new EventHandler(animation_Menu_Hide_FromDown_Completed_Settings);
            animation_Menu_Hide_FromDown.Begin();
        }

        void animation_Menu_Hide_FromDown_Completed_Settings(object sender, EventArgs e)
        {
            animation_Menu_Hide_FromDown.Completed -= new EventHandler(animation_Menu_Hide_FromDown_Completed_Settings);
            NavigationService.Navigate(new Uri("/Pages/SettingsPage.xaml", UriKind.Relative));
        }

        private void buttonHelp_Click(object sender, RoutedEventArgs e)
        {
            animation_Menu_Hide_FromDown.Completed += new EventHandler(animation_Menu_Hide_FromDown_Completed_Help);
            animation_Menu_Hide_FromDown.Begin();            
        }

        void animation_Menu_Hide_FromDown_Completed_Help(object sender, EventArgs e)
        {
            animation_Menu_Hide_FromDown.Completed -= new EventHandler(animation_Menu_Hide_FromDown_Completed_Help);
            NavigationService.Navigate(new Uri("/Pages/HelpPage.xaml", UriKind.Relative));
        }

        private void buttonHighScores_Click(object sender, RoutedEventArgs e)
        {
            animation_Menu_Hide_FromDown.Completed += new EventHandler(animation_Menu_Hide_FromDown_Completed_HighScores);
            animation_Menu_Hide_FromDown.Begin();            
        }

        void animation_Menu_Hide_FromDown_Completed_HighScores(object sender, EventArgs e)
        {
            animation_Menu_Hide_FromDown.Completed -= new EventHandler(animation_Menu_Hide_FromDown_Completed_HighScores);
            NavigationService.Navigate(new Uri("/Pages/HighScoresPage.xaml", UriKind.Relative));
        }

        private void buttonAbout_Click(object sender, RoutedEventArgs e)
        {            
            animation_Menu_Hide_FromDown.Completed += new EventHandler(animation_Menu_Hide_FromDown_Completed_About);
            animation_Menu_Hide_FromDown.Begin();
        }

        void animation_Menu_Hide_FromDown_Completed_About(object sender, EventArgs e)
        {
            animation_Menu_Hide_FromDown.Completed -= new EventHandler(animation_Menu_Hide_FromDown_Completed_About);
            NavigationService.Navigate(new Uri("/Pages/AboutPage.xaml", UriKind.Relative));
        }

        private void PhoneApplicationPage_OrientationChanged(object sender, OrientationChangedEventArgs e)
        {
            BitmapImage imageBackground;
            if ((e.Orientation & PageOrientation.Landscape) == (PageOrientation.Landscape))
            {
                if (this.ActualWidth > 0)
                {
                    Grid_Content.ColumnDefinitions[0].Width = new GridLength((this.ActualWidth - Grid_Content.Margin.Left - Grid_Content.Margin.Right) / 2);
                    Grid_Content.ColumnDefinitions[1].Width = new GridLength((this.ActualWidth - Grid_Content.Margin.Left - Grid_Content.Margin.Right) / 2);
                }
                else
                {
                    Grid_Content.ColumnDefinitions[0].Width = new GridLength((800 - Grid_Content.Margin.Left - Grid_Content.Margin.Right) / 2);
                    Grid_Content.ColumnDefinitions[1].Width = new GridLength((800 - Grid_Content.Margin.Left - Grid_Content.Margin.Right) / 2);
                }

                Grid.SetRow(buttonContinue, 0);      
                Grid.SetRow(buttonSettings, 1);
                Grid.SetRow(buttonHelp, 2);
                Grid.SetRow(buttonNewGame, 0);
                Grid.SetRow(buttonHighScores, 1);
                Grid.SetRow(buttonAbout, 2);

                Grid.SetColumn(buttonContinue, 0);
                Grid.SetColumn(buttonSettings, 0);
                Grid.SetColumn(buttonHelp, 0);
                Grid.SetColumn(buttonNewGame, 1);
                Grid.SetColumn(buttonHighScores, 1);
                Grid.SetColumn(buttonAbout, 1);

                imageBackground = new BitmapImage(new Uri("../Images/Backgrounds/Background_MainMenu_Landscape.png", UriKind.Relative));
            }
            else
            {
                Grid_Content.ColumnDefinitions[0].Width = new GridLength(1, GridUnitType.Star);
                Grid_Content.ColumnDefinitions[1].Width = new GridLength(0);

                Grid.SetRow(buttonContinue, 0);
                Grid.SetRow(buttonNewGame, 1);
                Grid.SetRow(buttonSettings, 2);
                Grid.SetRow(buttonHelp, 3);                
                Grid.SetRow(buttonHighScores, 4);
                Grid.SetRow(buttonAbout, 5);

                Grid.SetColumn(buttonContinue, 0);
                Grid.SetColumn(buttonNewGame, 0);
                Grid.SetColumn(buttonSettings, 0);
                Grid.SetColumn(buttonHelp, 0);                
                Grid.SetColumn(buttonHighScores, 0);
                Grid.SetColumn(buttonAbout, 0);

                imageBackground = new BitmapImage(new Uri("../Images/Backgrounds/Background_MainMenu_Portrait.png", UriKind.Relative));
            }

            if (imageBackground != null)
            {
                ((ImageBrush)LayoutRoot.Background).ImageSource = imageBackground;
            }
        }

        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = true;

            if (MessageBox.Show(ApplicationResources.Get("DoYouWantToExit"), "", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                animation_Menu_Hide_FromDown.Completed += new EventHandler(animation_Menu_Hide_FromDown_Completed_BackKeyPress);
                animation_Menu_Hide_FromDown.Begin();
            }
        }

        void animation_Menu_Hide_FromDown_Completed_BackKeyPress(object sender, EventArgs e)
        {
            animation_Menu_Hide_FromDown.Completed -= new EventHandler(animation_Menu_Hide_FromDown_Completed_BackKeyPress);

            App.Exit();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            animation_Menu_Show_FromUp.Begin();

            if (Loader.Load_SavedWaveType() == null)
            {
                buttonContinue.IsEnabled = false;
            }
            else
            {
                buttonContinue.IsEnabled = true;
            }

            base.OnNavigatedTo(e);
        }
    }
}
